package com.atguigu.szdz.dcsyl.service.impl;

import com.atguigu.szdz.dcsyl.dao.YlDataDao;
import com.atguigu.szdz.dcsyl.entity.*;
import com.atguigu.szdz.dcsyl.service.YlDataService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class YlDataServiceImpl implements YlDataService {

    @Autowired
    private YlDataDao ylDataDao;
    @Autowired
    private YlDataDao ylDataMapper;
    @Autowired
    private MapperUtils mapperUtils;
    @Override
    public void saveBatch(List<YlDataEntity> list) {
        ylDataMapper.batchInsert(list);
    }

    /**
     * 批量插入岩性数据
     * @param list
     */
    @Override
    public void saveBatchYx(List<YxDataEntity> list, Integer wellId) {
        ylDataMapper.batchInsertYx(list, wellId);
    }

    /**
     * 分页查询所有的岩性数据
     * @param wellId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getYxData(Integer wellId, Integer currentPage, Integer pageSize) {
        return mapperUtils.queryBySql("select * from dc_yxdata where well_id=" + wellId + " limit " + pageSize + " offset " + (currentPage - 1) * pageSize);
    }

    /**
     * 删除岩性数据
     * @param wellId
     */
    @Override
    public void removeYxData(Integer wellId) {
        mapperUtils.updataBySql("delete from dc_yxdata where well_id=" + wellId);
    }

    /**
     * 批量插入分层数据
     * @param list
     */
    @Override
    public void saveBatchFc(List<FcDataEntity> list, Integer wellId) {
        ylDataMapper.batchInsertFc(list, wellId);
    }
    /**
     * 分页查询所有的分层数据
     * @param wellId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getFcData(Integer wellId, Integer currentPage, Integer pageSize) {
        return mapperUtils.queryBySql("select * from dc_fcdata where well_id=" + wellId + " limit " + pageSize + " offset " + (currentPage - 1) * pageSize);

    }

    /**
     * 删除分层数据
     * @param wellId
     */
    @Override
    public void removeFcData(Integer wellId) {
        mapperUtils.updataBySql("delete from dc_fcdata where well_id=" + wellId);
    }
    /**
     * 批量插入其他数据
     * @param list
     */
    @Override
    public void saveBatchQt(List<QtDataEntity> list, Integer wellId) {
        ylDataMapper.batchInsertQt(list, wellId);
    }
    /**
     * 分页查询所有的其他数据
     * @param wellId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getQtData(Integer wellId, Integer currentPage, Integer pageSize) {
        return mapperUtils.queryBySql("select * from dc_qtdata where well_id=" + wellId + " limit " + pageSize + " offset " + (currentPage - 1) * pageSize);
    }
    /**
     * 删除其他数据
     * @param wellId
     */
    @Override
    public void removeQtData(Integer wellId) {
        mapperUtils.updataBySql("delete from dc_qtdata where well_id=" + wellId);
    }
    /**
     * 批量插入 wis 数据
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchWis(List<WisDataEntity> list, Integer wellId) {
        if (list == null || list.isEmpty()) {
            return;
        }

        // 分批处理，每批500条记录，避免PostgreSQL JDBC驱动报错
        int batchSize = 500;
        for (int i = 0; i < list.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, list.size());
            List<WisDataEntity> batchList = list.subList(i, endIndex);
            try {
                ylDataMapper.batchInsertWis(batchList, wellId);
            } catch (Exception e) {
                throw new RuntimeException("批量插入第" + (i/batchSize+1) + "批数据时出错: " + e.getMessage(), e);
            }
        }
    }
    /**
     * 分页查询所有的 wis 数据
     * @param wellId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getWisData(Integer wellId, Integer currentPage, Integer pageSize) {
        return mapperUtils.queryBySql("select * from dc_wisdata where well_id=" + wellId + " order by id asc "+ " limit " + pageSize + " offset " + (currentPage - 1) * pageSize);
    }
    /**
     * 删除 wis 数据
     * @param wellId
     */
    @Override
    public void removeWisData(Integer wellId) {
        mapperUtils.updataBySql("delete from dc_wisdata where well_id=" + wellId);
    }
    /**
     * 分页查询所有的 文本 数据
     * @param wellId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public List<Map<String, Object>> getWbData(Integer wellId, Integer currentPage, Integer pageSize) {
        return mapperUtils.queryBySql("select * from dc_wbdata where well_id=" + wellId + " order by id asc "+ " limit " + pageSize + " offset " + (currentPage - 1) * pageSize);

    }

    /**
     * 批量插入文本数据
     * @param list
     */
    @Override
    public void saveBatchWb(List<WbDataEntity> list, Integer wellId) {
        ylDataMapper.batchInsertWb(list, wellId);
    }

    /**
     * 删除文本数据
     * @param wellId
     */
    @Override
    public void removeWbData(Integer wellId) {
        mapperUtils.updataBySql("delete from dc_wbdata where well_id=" + wellId);
    }
    /**
     * 查询所有的压力数据
     * @return
     */
    @Override
    public List<YlDataEntity> getAllYlData() {
        return ylDataDao.queryAllPressureData();
    }

    @Override
    public List<YlDataDc> getAllYlDataDc(Integer wellId) {
        return ylDataDao.queryAllDcData(wellId);
    }
}
